import {useAsyncData} from "#app";
const baseUrl = 'http://localhost:9002/qcy-blog'

interface Options {
    baseURL?: string;
    body?: RequestInit["body"] | Record<string, any>;
    params?: Record<string, any>;
    query?: Record<string, any>;
    timeout?: number;
    method?: any;
    headers?:any;
}
function toCamelCase(str:string) {
    return str
        .split('/') // 按斜杠分割
        .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()) // 首字母大写
        .join(''); // 连接成字符串
}
const httpRequest = (url:string,options:Options)=>{
    return useAsyncData(toCamelCase(url), () => {
        return $fetch(url,{
            baseURL:'http://localhost:9002/qcy-blog',
            // baseURL:'https://www.qcycloud.com/qcy-blog',
            ...options,
            method:options.method||'GET',
            headers:options.headers,
            timeout:1000 * 60 * 5,
        });
    });
}
const myFetch = {
    get: (url:string,query?:any)=>{
        return httpRequest(url, {query});
    },
    post: (url:string,body?:any,options = {})=>{
        return httpRequest(url, {body,method:"POST",...options});
    }
}

export default  myFetch;
